package com.hs.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseXml {

	private List listDoc ;
	private Map lstMap;
	private String xpathDoc = "response/result/doc"; 
	private String xpathNum = "response/result/numFound"; 
	private String xpathLst = "response/lst"; 
	private String xpathNumFound = "response/result"; 
	/***
	 * 获取总数 属性名称
	 */
	private String numFoundName = "numFound";
	private SAXReader reader = new SAXReader();
	private int numFound;
	public  ParseXml(File file)
	{
		try {
			init(reader.read(file));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  ParseXml(String document)
	{   
		 
		try {
			Document doc = reader.read(new ByteArrayInputStream(document.getBytes("UTF-8")));
			this.init(doc);
		} catch (DocumentException e) { 
			System.out.println("解析xml数据文件错误" + e.getMessage());
		} catch (UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
	
	}
	
	/***
	 * ��ʼ��
	 * @param doc
	 */
	private void init(Document doc){
		
		this.setDoc(doc);
		this.setLst(doc);
		this.setNumFound(doc);
	}
	
	/**
	 * ���� Doc
	 * @param doc
	 */
	private void setDoc(Document doc)
	{
		 List list = doc.selectNodes(xpathDoc);
         Iterator it = list.iterator();
         this.listDoc = new ArrayList();
         while (it.hasNext()) {   
             Element elt = (Element) it.next();   
             Iterator iterator = elt.elements().iterator();
             Map map = new HashMap();
             while(iterator.hasNext())
             {
           	  Element eltChi = (Element)iterator.next();
           	  String  name =  eltChi.attributeValue("name");
           	  String  value = eltChi.getText();
           	  map.put(name, value);
             }
             listDoc.add(map);
          }
	}
	
	private void setNumFound(Document doc)
	{
	
		Element o = (Element)doc.selectObject(xpathNumFound);
		this.numFound = Integer.parseInt(o.attributeValue(numFoundName));
	}
	
	/**
	 * ����Lst
	 * @param doc
	 */
	private void setLst(Document doc){
		 List list = doc.selectNodes(xpathLst);
         Iterator it = list.iterator();
          this.lstMap = new HashMap();
         while (it.hasNext()) {
             Element elt = (Element) it.next();   
             setRecursive(elt);
          }
		
	}
	
	private void setRecursive(Element elt)
	{
		if(elt.elements().isEmpty())
        { 
     	  String  name =  elt.attributeValue("name");
      	  String  value = elt.getText();
      	  lstMap.put(name, value);
       	 
        }else
        {
            Iterator iterator = elt.elements().iterator();
            while(iterator.hasNext())
            {
          	  Element eltChi = (Element)iterator.next();
          	  this.setRecursive(eltChi);
            }
         }
	}
	
	public  ParseXml(OutputStream out)
	{
		
	}
	
	
	public List getDoc()
	{
		return listDoc;
	}
	
	
	
	public Map getLst()
	{
		return lstMap;
	}
	public static void main(String[] args){
		ParseXml p = new ParseXml(new File("D:\\program\\work-2008917\\preSpring\\src\\data.xml"));
		p.getDoc();
	}

	/**
	 * 获取总记录数
	 * @return
	 */
	public int getNumFound() {
		return numFound;
	}
}
